﻿<script type="text/javascript">


    ///<reference path="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js" />
    ///<reference path="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js" />
    ///<reference path="js/lib.js" />

    var _interval;
    $(document).ready()
    {

        GetJsonSproc("usp_GetCompletedDataRuns",

           function (json) {

               var sel = $("#DataRun1, #DataRun2");
               sel.empty();

               $.each(json, function (key, value) {
                   console.dir(value);
                   var o = $("<option>").text(value.Title).val(value.id);
                   sel.append(o);
               });

           });



        $("#KsTestResults").jsontable();

        UpdateKsTestResults();

    }


    function UpdateKsTestResults() {
        GetJsonSproc("usp_PendingGetKsTests",
            function (json) {

                $.each(json, function (key, item){
                    item.Significant = item.Significant == 1;
                    item.Completed = item.Completed == 1;
                });


                $("#KsTestResults").jsontable("option", {
                    source: json,
                    columnOrder: ["commandblock", "Title1", "Distance1", "Title2", "Distance2", "StartedProcessing", "Completed"],
                    columnDefs:
                        {
                            commandblock:
                                {
                                    Format: "commandblock",
                                    HeaderText: "",
                                    Items: [
                                        {
                                            Icon: "fa fa-trash-o",
                                            ActionName: "delete"
                                        }
                                    ],
                                    ItemClick: function(action, dataItem, tr)
                                    {
                                        switch (action)
                                        {
                                            case "delete":
                                            {
                                                if(confirm("Are you sure you wish to remove this KS Test?"))
                                                {
                                                    GetJsonSproc("usp_DeleteKsTest",
                                                        {
                                                            id: dataItem.id
                                                        },
                                                        function ()
                                                        {
                                                            tr.remove();
                                                            Growl("KS Test Removed.");
                                                        });
                                                }
                                                break;
                                            }
                                        }
                                    }
                                },
                            Title1: { HeaderText: "Run1" },
                            Title2: { HeaderText: "Run2" },
                            Distance1: { Format: "number" },
                           
                            Distance2: { Format: "number"},
                            PValue: { Format: "number" },
                            Significant: { Format: "boolean" },
                            Completed: { Format: "boolean" },
                            StartedProcessing: { Format: "datetime" },
                            FinishedProcessing: { Format: "datetime" }
                        }

                });
            });
    }


    function QueueKsTest()
    {
        var run1 = $("#DataRun1 > option:selected").val();
        var run2 = $("#DataRun2 > option:selected").val();


        GetJsonSproc("usp_AddKsTestRequest", 
            {
                DataRun1: run1,
                DataRun2: run2
            }, function (json) {
                Notify("KS Test Queued for processing.");
                UpdateKsTestResults();
            })
    }

</script>

<div class="ui-widget">
    <div class="ui-widget-header ui-corner-top">New KS Test</div>
    <div class="ui-widget-content pad ui-corner-bottom">

        <div class="ui-state-highlight ui-corner-all pad">
        
        Select the data runs to compare and click Queue. 
        </div>

        <section class="mt">
           <p>
                <b>Data Run 1: </b>
                <select id="DataRun1"></select>
           </p>
           
            <p>
                <b>Data Run 2:</b>               
                <select id="DataRun2"></select>
            </p>

           <p>
             <span class="button" onclick="QueueKsTest();" data-icon="fa-plus">Queue</span>
            </p>
          
        </section>
    </div>
</div>

<div class="ui-widget  mt" >
    <div class="ui-widget-header ui-corner-top">Pending K-S Tests <i class="fa fa-fw fa-refresh" onclick="UpdateKsTestResults();"></i></div>
    <div class="ui-widget-content ui-corner-bottom" id="KsTestResults"></div>
</div>